package com.eupmt.lab3.persistence.ibatis.dao;

import com.eupmt.lab3.domain.Article;
import com.ibatis.common.resources.Resources;
import com.ibatis.dao.client.DaoManager;
import com.ibatis.dao.client.DaoManagerBuilder;
import java.io.IOException;
import java.io.Reader;

/**
 *
 * @author sony
 */
public class ArticleDaoManager {

    protected static ArticleDaoManager instance = null;
    private ArticleDAO articleDAO = null;

    public ArticleDaoManager() throws IOException {
        super();
        Reader readerDao = Resources.getResourceAsReader("com/eupmt/lab3/persistence/ibatis/daoConfig.xml");
        DaoManager daoManager = DaoManagerBuilder.buildDaoManager(readerDao);
        this.articleDAO = (ArticleDAO) daoManager.getDao(ArticleDAO.class);
        //articleDAO=new ArticleDAOImpl();
    }

    // Mètodes d'Article
    public void deleteArticle(Article art) throws Exception {
        articleDAO.deleteArticle(art);
    }

    public void updateArticle(Article a) throws Exception {
        articleDAO.updateArticle(a);
    }

    public void insertArticle(Article a) throws Exception {
        articleDAO.insertArticle(a);
    }

    public Article getArticleById(int idArticle) throws Exception {
        return articleDAO.getArticleById(idArticle);
    }

    public void saveArticle(Article article) throws Exception {
        articleDAO.saveArticle(article);
    }

    public static ArticleDaoManager getInstance() throws IOException {
        if (instance == null) {
            instance = new ArticleDaoManager();
        }
        return instance;
    }
}
